package com.bbva.gptf.back.transformador;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.converter.Converter;

import com.bbva.gptf.back.util.Constantes;
import com.bbva.jee.arq.spring.core.log.I18nLog;
import com.bbva.jee.arq.spring.core.log.I18nLogFactory;

/**
 * @author Javier Estrella Rodriguez
 * @version 1.0
 * @created 08-ene-2013 01:22:43 p.m.
 */
public class ExcelToCSVTransformador implements Converter<File, File> {

	@Autowired
	@Qualifier("locale_ES")
	private transient Locale localizacion;

	private I18nLog log = I18nLogFactory.getLogI18n(EstatusCargaTransformador.class);
	
	private Workbook wb = null;
	private File fileCSV = null;
    private Row row = null;
    private FileOutputStream fostream = null;
    private OutputStreamWriter out = null;
    private String nombre = null;
    private POIFSFileSystem fileSystem = null;
    private InputStream is = null;
    private Sheet sheet = null;
    private String cell = null;
	
	@Override
	public File convert(File source) {		
		if(log.isTraceEnabled()) log.trace(localizacion, "entrada.metodo", "File convert(File source)");
		try {
			is = new FileInputStream(source);
			fileSystem = new POIFSFileSystem(is);
			wb = WorkbookFactory.create(fileSystem);
			nombre = source.getPath().replace(Constantes.FORMATEXCEL, Constantes.FORMATCSV);
			nombre = nombre.replace(".xls", ".csv");
			nombre = nombre.replace(" ", "_");
			fileCSV = new File(nombre);
			fostream = new FileOutputStream(fileCSV);
			out = new OutputStreamWriter(fostream, "UTF-8");
			sheet = wb.getSheetAt(0);
			Date fecha;
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");;
			for (int i = 0; i <= sheet.getLastRowNum(); i++) {
				row = sheet.getRow(i);
				for (int j = 0; j < row.getLastCellNum(); j++) {
					if(row.getCell(j)!=null){
						if (nombre.toUpperCase().contains("BENCHGLOBALDIARIO") && j == 0 && i > 0) {
							fecha = row.getCell(j).getDateCellValue();
							cell = dateFormat.format(fecha);
						} else {
							cell = row.getCell(j).toString();
						}
						
						out.write(cell);
					}else{
						out.write(Constantes.ESPACIO);
					}
					if(j+1 < row.getLastCellNum()){
	            		out.write("|");
	            	}
				}
				out.write("\r\n");
			}
			out.flush();
	        out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return fileCSV;
	}
}